﻿package net.wangit.dev.component;

import net.wangit.context.LocalContext;
import net.wangit.dev.ConsoleDomainUtil;
import net.wangit.jdbc.util.DBHelper;
import net.wangit.support.TreeFactory;
import net.wangit.view.tree.Tree;

public class ComponentTreeFactory extends TreeFactory {

	public static final String COMPONENT_ICON_PATH = "/domain/dev/c/";

	public ComponentTreeFactory() {
	}

	public Tree createTree(String treeDomain) throws Exception {
		net.wangit.context.ActionContext ac = LocalContext.getLocalContext()
				.getActionContext();
		String domain = ConsoleDomainUtil.getAndSetDomain();
		net.wangit.jdbc.DB db = DBHelper.getDomainDB(domain);
		Tree root = new Tree();
		root.setId("_c_");
		root.setName("组件");
		root.setIcon("/domain/dev/c/component.png");
		root.setAttribute("oncontextmenu=wm0()");
		Tree tableRoot = new Tree();
		tableRoot.setId("_r1");
		tableRoot.setIcon("/domain/dev/c/table.png");
		tableRoot.setAttribute("oncontextmenu=wmr1()");
		ObjectTree ot = new TableTree();
		int count[] = ot.addTree(ac, db, tableRoot);
		int realItemCount = ot.getItemsCount(db);
		tableRoot.setName(getName("表", count, realItemCount));
		root.add(tableRoot);
		Tree unitRoot = new Tree();
		unitRoot.setId("_r2");
		unitRoot.setIcon("/domain/dev/c/unit.png");
		unitRoot.setAttribute("oncontextmenu=wmr2()");
		ot = new UnitTree();
		count = ot.addTree(ac, db, unitRoot);
		realItemCount = ot.getItemsCount(db);
		unitRoot.setName(getName("单元", count, realItemCount));
		root.add(unitRoot);
		Tree enumerationRoot = new Tree();
		enumerationRoot.setId("_r3");
		enumerationRoot.setIcon("/domain/dev/c/enum.png");
		enumerationRoot.setAttribute("oncontextmenu=wmr3()");
		ot = new EnumerationTree();
		count = ot.addTree(ac, db, enumerationRoot);
		realItemCount = ot.getItemsCount(db);
		enumerationRoot.setName(getName("枚举", count, realItemCount));
		root.add(enumerationRoot);
		Tree treeRoot = new Tree();
		treeRoot.setId("_r4");
		treeRoot.setIcon("/domain/dev/c/tree.png");
		treeRoot.setAttribute("oncontextmenu=wmr4()");
		ot = new TreeTree();
		count = ot.addTree(ac, db, treeRoot);
		realItemCount = ot.getItemsCount(db);
		treeRoot.setName(getName("树", count, realItemCount));
		root.add(treeRoot);
		Tree chartRoot = new Tree();
		chartRoot.setId("_r5");
		chartRoot.setIcon("/domain/dev/c/chart.png");
		chartRoot.setAttribute("oncontextmenu=wmr5()");
		ot = new ChartTree();
		count = ot.addTree(ac, db, chartRoot);
		chartRoot.setName(getName("统计图", count, -1));
		root.add(chartRoot);
		Tree dpRoot = new Tree();
		dpRoot.setId("_r6");
		dpRoot.setIcon("/domain/dev/c/dp.png");
		dpRoot.setAttribute("oncontextmenu=wmr6()");
		ot = new DataPrivilegeTree();
		count = ot.addTree(ac, db, dpRoot);
		realItemCount = ot.getItemsCount(db);
		dpRoot.setName(getName("数据权限", count, realItemCount));
		root.add(dpRoot);
		Tree incrementorRoot = new Tree();
		incrementorRoot.setId("_r7");
		incrementorRoot.setIcon("/domain/dev/c/incrementor.png");
		incrementorRoot.setAttribute("oncontextmenu=wmr7()");
		ot = new IncrementorTree();
		count = ot.addTree(ac, db, incrementorRoot);
		incrementorRoot.setName(getName("自增器", count, -1));
		root.add(incrementorRoot);
		return root;
	}

	private static String getName(String title, int count[], int realItemCount) {
		int leafCount = count[0];
		StringBuffer name = new StringBuffer(title);
		name.append(" ").append(leafCount);
		if (realItemCount >= 0) {
			name.append("/").append(realItemCount);
			int treeItemCount = count[1];
			if (treeItemCount >= 0 && realItemCount != treeItemCount)
				name.append("/").append(realItemCount - treeItemCount);
		}
		return name.toString();
	}
}
